import tornado.ioloop
import tornado.web

class MainHandler(tornado.web.RequestHandler):
    def get(self):
        # 获取cookie
        cookie = self.get_secure_cookie("count")
        print(cookie)
        count = int(cookie) + 1 if cookie else 1
        count_string = "1 time" if count == 1 else "{} times".format(count)
        # 设置cookie
        self.set_secure_cookie("count",str(count))
        content = "你已经浏览了当前页面{}".format(count_string)
        self.write(content)

def make_app():
    secret = "S68FWQn+dyicA8HKWnyg8KxWdpL2QobMgY40X0GnSUWZWrN8vdmZrUsuLgb0Plal"
    return tornado.web.Application(
        handlers=[(r"/", MainHandler),],
        cookie_secret=secret
    )

if __name__ == "__main__":
    app = make_app()
    app.listen(8888)
    tornado.ioloop.IOLoop.current().start()
